package com.fan.springboot.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.fan.springboot.domain.Banner;
import com.fan.springboot.service.BannerService;
import com.fan.springboot.service.FileStore;
import com.fan.springboot.service.GlobalParams;

@RestController
@RequestMapping("/api")
public class BannerController {

	// 功能: 上传,保存,删除

	@Autowired
	private FileStore fileStore;

	@Autowired
	private BannerService bannerService;

	@RequestMapping(value = "/uploadBanner", method = RequestMethod.POST)
	public Object uploadWebFile(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {

		// 查询上一张图片的顺序
		int prevSequence = bannerService.getPrevSequence();

		// 先保存文件,取到返回值,再将记录插入数据库
		Map map = fileStore.uploadBy(file);
		Map<String, Object> resultMap = new HashMap<>();

		Banner banner = new Banner();
		String filePath = (String) map.get("filePath");

		banner.setFileName((String) map.get("fileName"));
		banner.setFilePath(filePath);
		banner.setIsUse(true);
		banner.setSequence(prevSequence + 1);
		String completePath = getFileBasePath(request) + filePath;
		banner.setCompletePath(completePath);
		bannerService.save(banner);

		resultMap.put(GlobalParams.SUCCESS, true);

		return resultMap;

	}

	@RequestMapping(value = "/getBannerList")
	public Object getBannerList(String orderby) {

		Map<String, Object> resultMap = new HashMap<>();
		List<Banner> bannerList = bannerService.findBannerListByOrderBy(orderby);
		resultMap.put("bannerList", bannerList);
		resultMap.put(GlobalParams.SUCCESS, true);

		return resultMap;

	}

	// 删除banner
	@RequestMapping(value = "/deleteBanner", method = RequestMethod.POST)
	public Object deleteBanner(String id) {

		Map<String, Object> resultMap = new HashMap<>();
		bannerService.delete(id);
		resultMap.put(GlobalParams.SUCCESS, true);

		return resultMap;

	}

	// 修改banner设置
	@RequestMapping(value = "/updateBanner", method = RequestMethod.POST)
	public Object updateBanner(@RequestBody List<Banner> list) {

		for (Banner banner : list) {
			bannerService.updateBanner(banner);
		}

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put(GlobalParams.SUCCESS, true);

		return resultMap;

	}

	public String getFileBasePath(HttpServletRequest request) {
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
		return basePath;
	}

}
